//
//  QJMineOrderFooter.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineOrderFooter.h"
#import "QJOrderPayVC.h"
#import "QJPointPayVC.h"
@implementation QJMineOrderFooter

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 取消订单
    self.cancelOrder.layer.borderWidth = 1;
    self.cancelOrder.layer.borderColor = YFColor_Text.CGColor;
    [self.cancelOrder setTitleColor:YFColor_Text forState:UIControlStateNormal];
    [self.cancelOrder setTitle:@"已取消" forState:UIControlStateDisabled];
    
    // 删除订单
    self.deleteOrder.layer.borderWidth = 1;
    self.deleteOrder.layer.borderColor = YFColor_Red.CGColor;
    [self.deleteOrder setTitleColor:YFColor_Red forState:UIControlStateNormal];
    self.deleteOrder.hidden = YES;
}

- (void)footerUpdateModel:(MineOrderModel *)model status:(NSString *)status{
    
    self.gopayBtn.layer.cornerRadius = 2.0;
    self.gopayBtn.layer.masksToBounds = YES;
    self.gopayBtn.layer.borderWidth = 1.0;
    self.gopayBtn.layer.borderColor = HexRGB(0xFF2600).CGColor;
    if (model) {
        self.model = model;
        
        NSInteger amount = 0;
        
        for (MineOrderDetailModel *detailModel in model.detailArr) {
            NSInteger num = [detailModel.amount integerValue];
            amount = amount + num;
        }
        self.shopNumberLabel.text = [NSString stringWithFormat:@"共%ld件商品,合计",(long)amount];
        self.shopTotalPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.total_amount];
    }
    if ([model.order_status isEqualToString:@"3"]) {//取消
        self.gopayBtn.hidden = YES;
        self.cancelOrder.enabled = NO;
        self.cancelOrder.layer.borderColor = YFColor_Text.CGColor;
        [self.cancelOrder setTitleColor:YFColor_Text forState:UIControlStateNormal];
        [self.cancelOrder setTitle:@"已取消" forState:UIControlStateDisabled];
        
    }else{
        
        self.gopayBtn.hidden = NO;
        self.cancelOrder.enabled = YES;
        self.cancelOrder.layer.borderColor = YFColor_Text.CGColor;
        [self.cancelOrder setTitleColor:YFColor_Text forState:UIControlStateNormal];
        [self.cancelOrder setTitle:@"取消订单" forState:UIControlStateNormal];
        
    }
    
}

- (IBAction)gopayBtnClickAction:(UIButton *)sender {
    
    MineOrderDetailModel *detailModel = (MineOrderDetailModel *)[self.model.detailArr objectAtIndex:0];
    if ([detailModel.is_points isEqualToString:@"0"]) {
        
        UIViewController *VC = [self currentViewController];
        QJOrderPayVC *vc = [[QJOrderPayVC alloc] init];
        vc.payOrderStr = self.model.order_id;
        vc.totalAmountStr = self.model.total_amount;
        
        vc.yeAmount = [detailModel.profit objectForKey:@"balance_income"];
        vc.silverAmount = [detailModel.profit objectForKey:@"balance_integral"];
        vc.pointsAmount = [detailModel.profit objectForKey:@"balance_user_integral"];
        if ([self.model.pay_code isEqualToString:@"alipayapp"]||[self.model.pay_code isEqualToString:@"appWeixinPay"]) {
            
            vc.pay_code = self.model.pay_code;
            
        }
        [VC.navigationController pushViewController:vc animated:YES];
        NSLog(@"去付款");
    }
    if ([detailModel.is_points isEqualToString:@"1"]) {
        
        UIViewController *VC = [self currentViewController];
        QJPointPayVC *vc = [[QJPointPayVC alloc] init];
        vc.payOrderStr = self.model.order_id;
        vc.totalAmountStr = self.model.total_amount;
        
        vc.silverAmount = [detailModel.profit objectForKey:@"balance_integral"];
        vc.pointsAmount = [detailModel.profit objectForKey:@"balance_user_integral"];
        if ([self.model.pay_code isEqualToString:@"alipayapp"]||[self.model.pay_code isEqualToString:@"appWeixinPay"]) {
            
            vc.payOrderStr = self.model.pay_code;
            
        }
        [VC.navigationController pushViewController:vc animated:YES];
        NSLog(@"去付款");
    }
    
}

- (IBAction)cancelOrderBtnClickAction:(UIButton *)sender {
   
    if (self.cancelOrderBlcokAction) {
        self.cancelOrderBlcokAction(self.model);
    }
}
- (IBAction)deleteOrderBtnClickAction:(UIButton *)sender {
    
}

//当前视图
-(UIViewController *)currentViewController{
    UIViewController *vc;
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[objc_getClass("UIViewController") class]] ) {
            vc=(UIViewController*)nextResponder;
            
            return vc;
        }
    }
    return vc;
}

@end
